package com.bw.login

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.base.app.RoutePath
import com.bw.common.mvi.BaseMviActivity
import com.bw.login.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RoutePath.PATH_LOGIN_PAGE)
class LoginActivity : BaseMviActivity<ActivityLoginBinding>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initViews() {

        //协议下加下滑线
        val agreementText = "登录则表明同意 用户协议 和 隐私协议"
        val spannableString = SpannableStringBuilder(agreementText)

        //登录
        val loginText ="以其他账号 登录"
        val loginString = SpannableStringBuilder(loginText)
        // 用户协议点击
        val userAgreementClick = object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 处理用户协议点击事件
                Toast.makeText(this@LoginActivity, "用户协议", Toast.LENGTH_SHORT).show()
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ContextCompat.getColor(this@LoginActivity, com.bw.common.R.color.gray) // 设置颜色
                ds.isUnderlineText = true // 添加下划线
            }
        }

        // 隐私协议点击
        val privacyAgreementClick = object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 处理隐私协议点击事件
                startActivity(Intent(this@LoginActivity,WebActivity::class.java))
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ContextCompat.getColor(this@LoginActivity, com.bw.common.R.color.gray)
                ds.isUnderlineText = true
            }
        }

        //其他登录点击
        val otherLoginClick = object : ClickableSpan() {
            override fun onClick(widget: View) {
                startActivity(Intent(this@LoginActivity,OtherActivity::class.java))
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ContextCompat.getColor(this@LoginActivity, com.bw.common.R.color.gray)
                ds.isUnderlineText = true
            }
        }

        // 设置点击范围
        spannableString.setSpan(
            userAgreementClick,
            agreementText.indexOf("用户协议"),
            agreementText.indexOf("用户协议") + "用户协议".length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        spannableString.setSpan(
            privacyAgreementClick,
            agreementText.indexOf("隐私协议"),
            agreementText.indexOf("隐私协议") + "隐私协议".length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )


        loginString.setSpan(
            otherLoginClick,
            loginText.indexOf("登录"),
            loginText.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        mDataBinding.tvAgreement.text = spannableString
        mDataBinding.tvAgreement.movementMethod = LinkMovementMethod.getInstance()
        mDataBinding.tvAgreement.highlightColor = Color.TRANSPARENT // 移除点击背景色


        //点击帮助
        mDataBinding.tvHelp.setOnClickListener {
            startActivity(Intent(this, HelpActivity::class.java))
        }

        mDataBinding.tvOtherLogin.text=loginString
        mDataBinding.tvOtherLogin.movementMethod = LinkMovementMethod.getInstance()
        mDataBinding.tvOtherLogin.highlightColor = Color.TRANSPARENT
    }

}

























